package courseagenda;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


import school.Duke;
import school.School;

public class CourseAgendaMain extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String SERVER_PROPERTIES = "server.properties";
	public String school;
	private Map<String, String> schoolToURL = new TreeMap<String, String>();
	
	public CourseAgendaMain() {
		super(new BorderLayout());

		String[] schoolStrings = { "Duke" };

		//Create the combo box, select the item at index 4.
		//Indices start at 0, so 4 specifies the pig.
		JComboBox schoolList = new JComboBox(schoolStrings);
		schoolList.addActionListener(this);

		//Lay out the demo.
		add(schoolList, BorderLayout.PAGE_START);
		setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
	}

	/** Listens to the combo box. */
	public void actionPerformed(ActionEvent e) {
		JComboBox cb = (JComboBox)e.getSource();
		school = (String)cb.getSelectedItem();
	}

	/**
	 * Create the GUI and show it.  For thread safety,
	 * this method should be invoked from the
	 * event-dispatching thread.
	 */
	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("School Course Sites");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create and set up the content pane.
		JComponent newContentPane = new CourseAgendaMain();
		newContentPane.setOpaque(true); //content panes must be opaque
		frame.setContentPane(newContentPane);

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public void setUpStudent(String username) {
		Properties studentProperties = new Properties();
		try {
			FileInputStream in = new FileInputStream(username+".properties");
			studentProperties.load(in);
			in.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	
	
	public void main(String[] args) {
		//Schedule a job for the event-dispatching thread:
		//creating and showing this application's GUI.
		createAndShowGUI();
		Properties schoolURLs = new Properties();
		try {
			FileInputStream in = new FileInputStream("server.properties");
			schoolURLs.load(in);
			in.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		String url = schoolURLs.getProperty(school);
		School schoolSite;
		if(url.contains("duke.edu")){
			schoolSite = new Duke(url);
			schoolSite.login();
		}
		
	}
}
